#define _CRT_SECURE_NO_WARNINGS 1

#include"seqlist.h"

void InitList(SeqList** plist)
{
	assert(plist);
	SeqList* p = (SeqList*)malloc(sizeof(SeqList));
	if (p == NULL)
	{
		perror("malloc:");
	}
	*plist = p;
	(*plist)->length = 0;
}

bool ListInsert(SeqList* plist, int i, ElemType value)
{
	assert(plist);
	if (i<1 || i>plist->length + 1 || plist->length == MAX)
	{
		return false;
	}
	i--;
	for (int j = plist->length; j > i; j--)
	{
		plist->arr[j] = plist->arr[j - 1];
	}
	plist->arr[i] = value;
	plist->length++;
	return true;
}

void DispList(SeqList* plist)
{
	assert(plist);
	for (int i = 0; i < plist->length; i++)
	{
		printf("%c ", plist->arr[i]);
	}
	printf("\n");
}

int ListLength(SeqList* plist)
{
	assert(plist);
	return plist->length;
}

bool ListEmpty(SeqList* plist)
{
	assert(plist);
	return plist->length == NULL;
}

bool GetElem(SeqList* plist, int i, ElemType* value)
{
	assert(plist);
	if (i<1 || i>plist->length)
	{
		return false;
	}
	*value = plist->arr[i - 1];
	return true;
}

int LocateElem(SeqList* plist, ElemType value)
{
	assert(plist);
	int i = 0;
	for (i = 0; i < plist->length; i++)
	{
		if (plist->arr[i] == value)
		{
			return i + 1;
		}
	}
	return 0;
}

bool ListDelete(SeqList* plist, int i)
{
	assert(plist);
	if (i<1 || i>plist->length)
	{
		return false;
	}
	i--;
	for (int j = i; j < plist->length; j++)
	{
		plist->arr[j] = plist->arr[j + 1];
	}
	plist->length--;
	return true;
}

void DestroyList(SeqList* plist)
{
	assert(plist);
	free(plist);
}